import Like, { type LikeAttributes, type LikeCreationAttributes } from '../models/Like'
import Post from '../models/Post'
import Comment from '../models/Comment'

export class LikeService {
  // 点赞/取消点赞
  static async toggleLike(
    userId: number,
    targetType: 'post' | 'comment',
    targetId: number
  ): Promise<{ liked: boolean; likeCount: number }> {
    // 检查是否已经点赞
    const existingLike = await Like.findOne({
      where: {
        userId,
        targetType,
        targetId
      }
    })

    if (existingLike) {
      // 取消点赞
      await existingLike.destroy()
      
      // 更新目标对象的点赞数量
      if (targetType === 'post') {
        await Post.decrement('likeCount', { where: { id: targetId } })
        const post = await Post.findByPk(targetId)
        return { liked: false, likeCount: post?.likeCount || 0 }
      } else {
        await Comment.decrement('likeCount', { where: { id: targetId } })
        const comment = await Comment.findByPk(targetId)
        return { liked: false, likeCount: comment?.likeCount || 0 }
      }
    } else {
      // 添加点赞
      await Like.create({
        userId,
        targetType,
        targetId
      })
      
      // 更新目标对象的点赞数量
      if (targetType === 'post') {
        await Post.increment('likeCount', { where: { id: targetId } })
        const post = await Post.findByPk(targetId)
        return { liked: true, likeCount: post?.likeCount || 0 }
      } else {
        await Comment.increment('likeCount', { where: { id: targetId } })
        const comment = await Comment.findByPk(targetId)
        return { liked: true, likeCount: comment?.likeCount || 0 }
      }
    }
  }

  // 检查用户是否已点赞
  static async isLiked(
    userId: number,
    targetType: 'post' | 'comment',
    targetId: number
  ): Promise<boolean> {
    const like = await Like.findOne({
      where: {
        userId,
        targetType,
        targetId
      }
    })
    return !!like
  }

  // 获取用户的点赞列表
  static async getUserLikes(
    userId: number,
    targetType?: 'post' | 'comment',
    page: number = 1,
    pageSize: number = 10
  ): Promise<{ likes: Like[]; total: number }> {
    const offset = (page - 1) * pageSize
    const where: any = { userId }
    
    if (targetType) {
      where.targetType = targetType
    }
    
    const { count, rows } = await Like.findAndCountAll({
      where,
      order: [['createdAt', 'DESC']],
      limit: pageSize,
      offset
    })

    return {
      likes: rows,
      total: count
    }
  }

  // 获取目标的点赞用户列表
  static async getLikedUsers(
    targetType: 'post' | 'comment',
    targetId: number,
    page: number = 1,
    pageSize: number = 10
  ): Promise<{ users: any[]; total: number }> {
    const offset = (page - 1) * pageSize
    
    const { count, rows } = await Like.findAndCountAll({
      where: {
        targetType,
        targetId
      },
      include: [
        {
          model: require('../models/User').default,
          as: 'user',
          attributes: ['id', 'username', 'name']
        }
      ],
      order: [['createdAt', 'DESC']],
      limit: pageSize,
      offset
    })

    return {
      users: rows.map(like => like.user),
      total: count
    }
  }
}

export default LikeService
